package com.puzzlebazar.client.services;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public class ServiceFactory {

	private static UserInfoServiceAsync userService = null;
	private static SettingsServiceAsync settingsService = null;
	private static PuzzleServiceAsync puzzleService = null;
	
	/**
	 * Returns the user service, create it if needed 
	 * 
	 * @return The user service
	 */
	public static UserInfoServiceAsync getUserInfoService() {

		if( userService != null ) return userService;
		
		userService = (UserInfoServiceAsync)GWT.create(UserInfoService.class);
	    ServiceDefTarget serviceEndpoint = (ServiceDefTarget) userService;
	    String serviceRelativeURL = GWT.getModuleBaseURL() + "user";
	    serviceEndpoint.setServiceEntryPoint(serviceRelativeURL);
		
	    return userService;
	}
	
	/**
	 * Returns the admin service, create it if needed 
	 * 
	 * @return The admin service
	 */
	public static SettingsServiceAsync getSettingsService() {

		if( settingsService != null ) return settingsService;
		
		settingsService = (SettingsServiceAsync)GWT.create(SettingsService.class);
	    ServiceDefTarget serviceEndpoint = (ServiceDefTarget) settingsService;
	    String serviceRelativeURL = GWT.getModuleBaseURL() + "settings";
	    serviceEndpoint.setServiceEntryPoint(serviceRelativeURL);
		
	    return settingsService;
	}

	/**
	 * Returns the puzzle service, create it if needed 
	 * 
	 * @return The puzzle service
	 */
	public static PuzzleServiceAsync getPuzzleService() {

		if( puzzleService != null ) return puzzleService;
		
		puzzleService = (PuzzleServiceAsync)GWT.create(PuzzleService.class);
	    ServiceDefTarget serviceEndpoint = (ServiceDefTarget) puzzleService;
	    String serviceRelativeURL = GWT.getModuleBaseURL() + "puzzle";
	    serviceEndpoint.setServiceEntryPoint(serviceRelativeURL);
		
	    return puzzleService;
	}
    
}
